<?php
namespace app\admin\model;
use app\common\model\Store as Model;
use think\facade\Db;
use think\facade\Log;

/**
 * 门店表
 */
class Store extends Model
{
    /**
     * 保存并更新
     * @param $param
     * @return array
     */
    static function savedata($param)
    {
        Db::startTrans();
        try {
            if (empty($param['password'])) {
                unset($param['password']);
            }
            // 更新Store表
            $model = new self();
            // 更新商户
            if(!empty($param['id'])){
                unset($param['create_time']);
                $model = self::find($param['id']);
                $result = $model->save($param);
                if($result == false){
                    Db::rollback();
                    return jsondata(1,"操作失败1001");
                }
                // 修改关联部门
            // 添加商户
            }else{
                // 新增商户
                $result = self::create($param);
                if($result == false){
                    Db::rollback();
                    return jsondata(1,"操作失败1002");
                }
                // 读取套餐角色
                $roles = StoreSetmeal::where('id',$param['setmeal_id'])->value('roles');
                // 新增角色
                $role = StoreRole::create([
                    'store_id'    => $result->id,
                    'name'        => "管理员",
                    'status'      => 1,
                    'is_admin'    => 1,
                    'description' => '系统角色,不可修改,删除!',
                    'roles'       => explode(',', $roles)
                ]);
                if($role == false){
                    Db::rollback();
                    return jsondata(1,"操作失败1003");
                }
                // 新增部门
                $group = StoreGroup::create([
                    'store_id'    => $result->id,
                    'name'        => "总经办",
                    'status'      => 1,
                    'is_admin'    => 1,
                    'description' => '系统部门,不可修改,删除!',
                ]);
                if($group == false){
                    Db::rollback();
                    return jsondata(1,"操作失败1003");
                }
                Log::write('212');
                // 新增管理员
                $user = StoreUser::create([
                    'store_id'   => $result->id,
                    'roles_id'   => explode(',', $role->id),
                    'group_id'   => $group->id,
                    'name'       => $param['contact'],
                    'username'   => $param['username'],
                    'mobile'     => $param['mobile'],
                    'password'   => $param['password'],
                    'remark'     => '超级管理员,不可删除!',
                    'is_admin'   => 1,
                ]);
                if($user == false){
                    Db::rollback();
                    return jsondata(1,"操作失败1004");
                }
                // 更新超级管理员信息
                self::where('id',$result->id)->update([
                    'admin_id'       => $user->id,
                    'admin_roles_id' => $role->id,
                    'admin_group_id' => $group->id,
                ]);
            }
            // 提交事务
            Db::commit();
            return jsondata(0,"操作成功");
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return jsondata(1,"操作失败1000");
        }
    }



}